package com.tansun.easycare.rule.table.singletblindex.web;

import java.util.List;

import com.tansun.easycare.rule.common.enums.RuleModelTypeFileEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.common.web.model.ProjectInfo;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleversion.entity.RuleVersioninfo;
import com.tansun.easycare.rule.table.decisiontable.service.DecisionTableService;
import com.tansun.easycare.rule.table.singletblindex.model.IndexDetailVO;
import com.tansun.easycare.rule.table.singletblindex.model.RuleTbleManage;
import com.tansun.easycare.rule.table.singletblindex.model.SingleTblIndexEntity;
import com.tansun.easycare.rule.table.singletblindex.model.SingleTblIndexRow;
import com.tansun.easycare.rule.table.singletblindex.service.SingleTblIndexService;
import com.tansun.rule.utils.LoggerManager;
import com.tansun.rule.utils.StringUtils;

@RestController
@RequestMapping(value = "/ruleProj/singleTblIndex")
public class SingleTblIndexController extends RuleBaseController {

    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private SingleTblIndexService singleTblIndexService;
    @Autowired
    private DecisionTableService decisionTableService;

    /**
     * 一维表索引模型列表
     *
     * @param projectCode 项目code
     * @param param       一维表索引模型
     * @return 一维表索引路径
     */
    @RequestMapping("index")
    public ResponseTable<RuleManageVO> index(@RequestBody RuleManageVO param) throws Exception {
        PageInfo<RuleManageVO> pageInfo = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        ResponseTable<RuleManageVO>  resVo  = new  ResponseTable<RuleManageVO>(pageInfo);
        return resVo;
    }

    /**
     * 新增一维表索引OR一维表索引编辑
     *
     * @param indexId 一维表索引id
     * @param isedit  是否可编辑
     * @return 进入模型编辑页面
     */
    @RequestMapping(value = "detail")
    public ResponseVO detail(@RequestBody ProjectInfo projectInfo) throws Exception {
    	ResponseVO  res  = new ResponseVO();
        RuleManage indexModel = null;
        if (StringUtils.isEmpty(projectInfo.getManageId())) {
        	indexModel  =  new  RuleManage();
        }else {
            indexModel = this.ruleManageService.getById(projectInfo.getManageId());
            if(indexModel==null){
            	indexModel = ruleManageService.findByCode(projectInfo.getManageId(), projectInfo.getProjectCode());
            }     	
        }    		
        if(StringUtils.isNotEmpty(projectInfo.getVersion())){
        	indexModel.setVersion(projectInfo.getVersion());
        }
        RuleTbleManage  ruleTbl = new  RuleTbleManage();
        ruleTbl.setIsedit(projectInfo.getIsedit());
        ruleTbl.setRankRelationId(projectInfo.getRankRelationId());
        ruleTbl.setRuleManage(indexModel);
        res.setData(ruleTbl);
        return  res;        

    }

    /**
     * 获取一维表索引模型详情
     *
     * @param indexId 一维表索引id
     * @param version 版本号
     * @return 一维表索引详情，包含模型信息和规则信息
     */
    @RequestMapping(value = "getIndexDetail")
    public ResponseVO getIndexDetail(@RequestBody IndexDetailVO  indexDetail) throws Exception {
    	ResponseVO ret = new ResponseVO();
        try {
            RuleManage indexModel = this.ruleManageService.getById(indexDetail.getIndexId());
            String  version = indexDetail.getVersion();
            if (StringUtils.isEmpty(version)) version = indexModel.getVersion();
            if("1".equals(indexDetail.getTemp())) {
    			version = indexModel.getActiveVersion();
    		}
            List<SingleTblIndexRow> indexInfoList = singleTblIndexService.findTblIndexRowList(indexDetail.getIndexId(), version);
//            for (SingleTblIndexRow indexRow : indexInfoList) {
//                List<SingleTblIndexItem> itList = indexRow.getSingleTblRow();
//                for (SingleTblIndexItem indexItem : itList) {
//                	if("aging".equals(indexItem.getNameEN())) {
//                		indexItem.setIsfuncparam("1");
//                	}
//                }
//            }
            SingleTblIndexEntity entity = new SingleTblIndexEntity();
            entity.setIndexModel(indexModel);
            entity.setSingleTblIndexList(indexInfoList);
            RuleVersioninfo  ruleVersionInfo = this.ruleManageService.findRuleVersionInfo(indexModel.getProjectCode(), indexModel.getId());
            entity.setDatasetNames(ruleVersionInfo.getDatasetNames());
            ret.setData(entity);
        } catch (Exception e) {
            LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setStatus(ResponseVO.ERROR);;
            ret.setDescription("一维表索引信息获取失败");
        }
        return ret;
    }

    /**
     * 保存或更新评分模型
     *
     * @param indexEntity 一维表索引实体
     * @return 保存一维表模型信息
     */
    @RequestMapping("saveOrUpdate")
    public ResponseVO saveOrUpdate(@RequestBody SingleTblIndexEntity indexEntity) throws Exception {
    	ResponseVO ret = new ResponseVO();
        try {
            Integer num = this.ruleManageService.countListFindName(indexEntity.getIndexModel(), RuleModelTypeFileEnum.SingleTblIndex.type);
            if (num != null && num>0) {
                ret.setStatus(ResponseVO.ERROR);
                ret.setDescription("变量名称重复，请使用其他有效名称");
                return ret;
            }
            singleTblIndexService.saveOrUpdate(indexEntity);
        } catch (Exception e) {
            LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setDescription(e.getMessage());
            ret.setStatus(ResponseVO.ERROR);
        }
        return ret;
    }
    
    /**
     * 显示java代码
     *
     * @param indexEntity 一维表索引实体
     * @return 保存一维表模型信息
     */
    @RequestMapping("showJavaCode")
    public @ResponseBody
    ResMessage showJavaCode(@RequestBody SingleTblIndexEntity indexEntity) throws Exception {
    	ResMessage res = new ResMessage();
    	String javacode = singleTblIndexService.showJavaCode(indexEntity);
        res.setCode("success");
        res.setMsg(javacode);
        return res;
    }
    
    @Override
    public void setRuleBizService() {
        super.setRuleBaseService(this.decisionTableService);
    }
    
    @RequestMapping("copyRule")
    public ResponseVO copyRule(@RequestBody  ProjectInfo  projectInfo) {
    	ResponseVO ret = new ResponseVO();
        try {
            this.decisionTableService.copyRule(projectInfo.getManageId(),projectInfo.getRankRelationId());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setStatus(ResponseVO.ERROR);
            ret.setDescription(e.getMessage());
        }
        return ret;
    }

}
